import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Mash {
	private final int cap;
	private List<Knoten> kList;
	Lock lock = new ReentrantLock();

	public Mash(int cap){
		this.cap = cap;
		kList = new CopyOnWriteArrayList<Knoten>();
	}

	public Mash(int cap, List<Knoten> neu){
		this.cap = cap;
		this.kList = new CopyOnWriteArrayList<Knoten>();
		for (Knoten knoten : neu){
			this.add(knoten);
		}
	}

	public synchronized boolean tryLock(){
		return lock.tryLock();
	}
	
	public synchronized void testLock(){
		while(!lock.tryLock())
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	public synchronized void lock(){
		lock.lock();
	}

	public synchronized void unlock(){
		lock.unlock();
		notifyAll();
	}

	public synchronized void add(Knoten k){
		kList.add(k);
		k.setMash(this);
	}

	public synchronized List<Knoten> getkList() {
		return kList;
	}

	public synchronized int getCap() {
		return cap;
	}

	public synchronized void removeKnList(List<Knoten> toRemove){
		for(Knoten k : toRemove){
			kList.remove(k);
			k.setMash(null);
		}
	}

	public synchronized boolean zeigtAufMash(Knoten tocheck){
		for (Knoten k : kList){
			for(Knoten akt : k.getVorganger()){
				if(tocheck.equals(akt)) return true;
			}
		}
		return false;
	}
	
	public synchronized boolean isempty(){
		return kList.isEmpty();
	}

	@Override
	public synchronized String toString(){
		StringBuffer sb = new StringBuffer();
		for (Knoten k : kList){
			sb.append("" + k + "");
		}
		return sb.toString();
	}

}
